﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Linq;
using Vaffanculo.Common.Constants;
using Vaffanculo.Migration.DataBase;
using Vaffanculo.Migration.SchemaMigration;
using Vaffanculo.Migration.ImplementsSchema.Excel;
using log4net;


namespace Vaffanculo.Migration.ImplementsSchema
{
    public class InitMigration
    {
        public IDataBaseTarget DataBaseTarget { get; private set; }
        public ISchemaXmlMigration SchemaXmlMigration { get; set; }

        public ReadFileExcel ReadFileExcel{ get; set; }
        //todo: el log debe ser hecho de forma que sea para todo el sistema
        public static readonly ILog Log = LogManager.GetLogger(typeof(InitMigration));


        public InitMigration(string pathXml)
        {
            log4net.Config.XmlConfigurator.Configure();
            NameValueCollection appSettings = ConfigurationManager.AppSettings;

            string databasemanage = appSettings.Get("DataBaseManager");
            switch (databasemanage)
            {
                case "Postgresql":
                    DataBaseTarget = new DataBasePostgres
                                         {
                                             ConnectionString =
                                                 String.Format(
                                                     "Server={0};Port={1};Database={2};User={3};Password={4};Encoding={5};Timeout={6};",
                                                     appSettings.Get("IPAddress"), appSettings.Get("Port"),
                                                     appSettings.Get("DataBase"), appSettings.Get("User"),
                                                     appSettings.Get("Password"), appSettings.Get("Encoding"),
                                                     appSettings.Get("Timeout"))
                                         };
                    break;
                    
            }

            SchemaXmlMigration = new XmlMigration {PathXml = pathXml};
            SchemaXmlMigration.ReadMapXml();



            ReadFileExcel = new ReadFileExcel {DataBaseTarget = DataBaseTarget};

            foreach (ISchemaFile schemaFile in SchemaXmlMigration.FilesSourceMigration)
            {
                
                List<long> dates = SequenceList(schemaFile.Cycle, schemaFile.DateTimeStart, schemaFile.DateTimeEnd);
                foreach (long date in dates)
                {
                    string path = DatePattern(schemaFile.PathFile + @"\" + schemaFile.FilePattern, new DateTime(date));
                    ReadFileExcel.PaternPath = new DateTime(date).ToString("yyyy-MM-dd");

                    ReadFileExcel.ReadDimentionOrFact(schemaFile.Dimentions, schemaFile.Facts,path,schemaFile.Sheet);
                }
                /*
                 * initMigration.SequenceList(initMigration.SchemaXmlMigration.FilesSourceMigration[0].Cycle,
                                                       initMigration.SchemaXmlMigration.FilesSourceMigration[0].
                                                           DateTimeStart,
                                                       initMigration.SchemaXmlMigration.FilesSourceMigration[0].
                                                           DateTimeEnd).Count);
                 */
                    
            }
            

        }

        public void TestConection()
        {
            DataBaseTarget.ExecuteNonQuery("Select * FROM dim_centrales_sistema");
        }

        public List<long> SequenceList(string cycle, DateTime dateTimeStart, DateTime dateTimeEnd)
        {
            var result = new List<long>();
            if (dateTimeEnd.Equals(dateTimeStart))
            {
                result.Add(dateTimeStart.Ticks);
                return result;
            }
            switch (cycle)
            {
                case CicleTimeConstants.Hour:
                    while (dateTimeStart<dateTimeEnd)
                    {
                        result.Add(dateTimeStart.Ticks);
                        dateTimeStart = dateTimeStart.AddHours(1);
                    }
                    break;
                case CicleTimeConstants.Daily:
                    while (dateTimeStart<=dateTimeEnd)
                    {
                        result.Add(dateTimeStart.Ticks);
                        dateTimeStart = dateTimeStart.AddDays(1);
                    }
                    break;
                case CicleTimeConstants.Fortnightly:
                    while (dateTimeStart<=dateTimeEnd)
                    {
                        result.Add(dateTimeStart.Ticks);
                        dateTimeStart = dateTimeStart.AddDays(15);
                    }
                    break;
                case CicleTimeConstants.Monthly:
                    while (dateTimeStart<=dateTimeEnd)
                    {
                        result.Add(dateTimeStart.Ticks);
                        dateTimeStart = dateTimeStart.AddMonths(1);
                    }
                    break;
                case CicleTimeConstants.Bimonthly:
                    while (dateTimeStart<dateTimeEnd)
                    {
                        result.Add(dateTimeStart.Ticks);
                        dateTimeStart = dateTimeStart.AddMonths(2);
                    }
                    break;
                case CicleTimeConstants.Semester:
                    while (dateTimeStart<dateTimeEnd)
                    {
                        result.Add(dateTimeStart.Ticks);
                        dateTimeStart = dateTimeStart.AddMonths(6);
                    }
                    break;
                case CicleTimeConstants.Yearly:
                    while (dateTimeStart<dateTimeEnd)
                    {
                        result.Add(dateTimeStart.Ticks);
                        dateTimeStart = dateTimeStart.AddYears(1);
                    }
                    break;
            }
            return result;
        }


        public string DatePattern(string pathFile, DateTime dateTime)
        {
            return pathFile.Replace("YYYY", dateTime.ToString("yyyy")).Replace("YY", dateTime.ToString("yy")).
            Replace("MMMM", dateTime.ToString("MMMM")).Replace("MMM", dateTime.ToString("MMM")).
            Replace("MM", dateTime.ToString("MM")).
            Replace("DDDD", dateTime.ToString("dddd")).Replace("DDD", dateTime.ToString("ddd")).
            Replace("DD", dateTime.ToString("dd"));
        }

        private string ExcludeLetter(string stringReplace)
        {
            return stringReplace.Where(c => c.Equals('1') || c.Equals('2') || c.Equals('3') || c.Equals('4') || c.Equals('5') || c.Equals('6') || c.Equals('7') || c.Equals('8') || c.Equals('9') || c.Equals('0') || c.Equals('-') ).Aggregate(string.Empty, (current, c) => current + c);
         
        }
    }
}
